/**
 * Created with File
 * Author: Chuans
 * Github: https://github.com/chuans
 * Date: 2018/9/6
 * Time: 下午4:59
 */

import {observable, action, runInAction} from 'mobx';
import http from '../../util/axios';
import {CoinTarget} from '../../enum/coins'
import APIS from '../../config/apis'


class CoinList {
    // 数据列表
    @observable public list: Table.Data[] = [];

    // 保留搜索状态
    @observable public searchStatus: any = {
        pageIndex: 1,
        pageSize: 10,
    };

    // 存当前选择的用户ID  或者所有人
    @observable public currentSelect: string | number = '';

    @observable public total: number = 1;

    // 是否在加载中
    @observable public listLoading: boolean = false;

    // 安全码的弹框
    @observable public modalVisible: boolean = false;

    // 提交中的状态
    @observable public addLoading: boolean = false;

    // 加载列表
    @action public getList = async (values: Table.Values) => {
        this.listLoading = true;

        const result = await http.get(APIS.COIN_LIST, values);


        if (result.code !== 0) {
            runInAction(() => {
                this.listLoading = false;
            })
            return result.message;
        }
        runInAction(() => {
            const {data, total} = result.data;

            this.listLoading = false;
            this.list = data;
            this.total = total;
            this.searchStatus = values;
        })
    }


    /**
     * 添加金币接口
     * @desc 需要判断当前添加的类型 是所有人 还是指定某个人  当为-1000的时候 表示所有人
     * @param values
     */
    @action public addCoin = async (values: any) => {
        this.addLoading = true;

        values.user = this.currentSelect;

        values.target = this.currentSelect === -1000 ? CoinTarget.all_n : CoinTarget.assign_n;

        const result = await http.post(APIS.COIN_ADD, values);

        if (result.code !== 0) {
            runInAction(() => {
                this.addLoading = false;
            })
            return result.message;
        }

        runInAction(() => {
            this.addLoading = false;
        });

        return false;
    }

    // 设置安全码输入的可见性
    @action public setVisible = (visible = false) => {
        this.modalVisible = visible;
    }

    // 设置当前选择的用户ID
    @action public setCurrentSelect = (val: string | number): void => {
        this.currentSelect = val;
    }
}

export default new CoinList();
